package com.ks.service;

import com.ks.base.BaseService;
import com.ks.dao.RoleDao;
import com.ks.dao.UnitDao;
import com.ks.dao.UserDao;
import com.ks.dao.UserRoleDao;
import com.ks.entity.Role;
import com.ks.entity.Unit;
import com.ks.entity.User;
import com.ks.entity.UserRole;
import com.ks.util.StringUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserService<T> extends BaseService<T> {
    private final static Logger log = Logger.getLogger(UserService.class);

    @Autowired
    private UserDao<T> dao;
    @Autowired
    private UnitDao<T> unitDao;
    @Autowired
    private RoleDao<T> roleDao;
    @Autowired
    private UserRoleDao<T> userRoleDao;

    public UserDao<T> getDao() {
        return dao;
    }

    public T queryByAccount(Object o) {
        return dao.queryByAccount(o);
    }

    public Map<String, Object> login(HttpServletRequest request, String account, String password) {
        Map<String, Object> map = new HashMap<String, Object>();
        String md5Password = StringUtil.MD5(password);
        Map<String, Object> paramap = new HashMap<String, Object>();
        paramap.put("account", account);
        int userCount = getDao().queryByCount(paramap);
        if (userCount == 1) {
            paramap.put("password", md5Password);
            List<T> list = getDao().queryList(paramap);
            if (list.size() == 1) {
                map.put("status", "1");
                map.put("error", "成功");
                HttpSession session = request.getSession(true);
                Map<String, Object> urmap = new HashMap<>();
                User user = (User) list.get(0);
                urmap.put("user_id", user.getId());
                List<T> ur = userRoleDao.queryList(urmap);

                JSONArray json = new JSONArray();
                Map<String, Object> namemap = new HashMap<>();

                for (T t : ur) {
                    UserRole entity = (UserRole) t;
                    json.add(entity.getRole_id());
                    Role role = (Role) roleDao.queryById(entity.getRole_id());
                    namemap.put(entity.getRole_id().toString(), role.getName());
                }
                map.put("role", json);
                map.put("name", namemap);

                session.setAttribute("userInfo", user);
            } else {
                map.put("status", "0");
                map.put("error", "密码不正确");
            }
        } else {
            map.put("status", "0");
            map.put("error", "用户名不正确");
        }

        return map;
    }

    public String togo(String roleid) {
        // TODO Auto-generated method stub
        return "static/pages/roleMng/role-list";
    }

    public void updatapassword(String password,String id){
        Map<String,Object> map = new HashMap<>();
        map.put("password",password);
        map.put("id",id);
        dao.updateBySelective(map);
    }

    public List< Map<String,Object>> searchAjax(String unitIds,String proIds, String rankIds){
        String[] unitId =null;
        if (StringUtils.isNotBlank(unitIds)) {
            unitId = unitIds.split(",");
        }

        String[] proId =null;
        if (StringUtils.isNotBlank(proIds)) {
            proId = proIds.split(",");
        }

        String[] rankId =null;
        if (StringUtils.isNotBlank(rankIds)) {
            rankId = rankIds.split(",");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("unitId",unitId);
        map.put("proId",proId);
        map.put("rankId",rankId);


        List<Map<String, Object>> units = dao.findByUnitIdsAndRankIdsAndProIds(map);
        for (Map<String, Object> mapUnit : units) {
            //部门
            Unit unit = (Unit) unitDao.queryById(mapUnit.get("unit_id"));
            mapUnit.put("unit",unit.getName());
            //角色
            // 角色
            List<Role> roleList = (List<Role>) roleDao.queryListByUserId(mapUnit.get("id"));
            String roleStr = "";
            for (Role role : roleList) {
                roleStr = roleStr + role.getName() + ";";
            }
            mapUnit.put("role",roleStr);
        }
        return units;
    }

    public List<User> findAll(Object o){
        return dao.findAllDesc(o);
    }
}
